package jnpf.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PaginationVO;
import jnpf.constant.MsgCode;
import jnpf.emnus.FilePreviewTypeEnum;
import jnpf.file.YozoApi;
import jnpf.model.SuffixParams;
import jnpf.model.YozoFileParams;
import jnpf.model.YozoParams;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 文档在线编辑
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021/5/7
 */
@Tag(name = "文档在线预览编辑", description = "DocumentEdit")
@RestController
@RequestMapping("/DocumentEdit")
public class DocumentEditController {

    @Autowired
    private YozoApi yozoApi;

    /**
     * 上传本地文件
     *
     * @param file 文件
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadLocalFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传本地文件")
    public ActionResult yozoUploadLocalFile(MultipartFile file) throws IOException {
        return yozoApi.upload(file);
    }

    /**
     * 新建文件
     *
     * @param fileName     名称
     * @param templateType 类型
     * @return
     */
    @GetMapping("/create")
    @Operation(summary = "新建文件")
    @Parameters({
            @Parameter(name = "fileName", description = "名称"),
            @Parameter(name = "templateType", description = "类型"),
    })
    public ActionResult newCreate(@RequestParam("fileName") String fileName, @RequestParam("templateType") String templateType) {
        return yozoApi.newCreate(fileName, templateType);
    }

    /**
     * 删除文件版本
     *
     * @param fileVersionId 主键
     * @return
     */
    @GetMapping("/deleteVersion")
    @Operation(summary = "删除文件版本")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult deleteVersion(@RequestParam("fileVersionId") String fileVersionId) {
        return yozoApi.deleteVersion(fileVersionId);
    }

    /**
     * 批量删除文件版本
     *
     * @param fileVersionIds 批量主键
     * @return
     */
    @GetMapping("/batchDelete")
    @Operation(summary = "批量删除文件版本")
    @Parameters({
            @Parameter(name = "fileVersionIds", description = "批量主键"),
    })
    public ActionResult batchDelete(@RequestParam("fileVersionIds") String[] fileVersionIds) {
        return yozoApi.batchDelete(fileVersionIds);
    }

    /**
     * 在线编辑
     *
     * @param fileVersionId 主键
     * @return
     */
    @GetMapping("/onlineEditFile")
    @Operation(summary = "在线编辑")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult onlineEditFile(@RequestParam("fileVersionId") String fileVersionId) {
        return yozoApi.editFile(fileVersionId);
    }

    /**
     * 文档列表
     *
     * @param pageModel 分页模型
     * @return
     */
    @PostMapping("/documentList")
    @Operation(summary = "文档列表")
    public ActionResult documentList(PaginationVO pageModel) {
        return yozoApi.documentList(pageModel);
    }

    /**
     * 本地同步编辑过后的文件
     *
     * @param fileVersionId 主键
     * @return
     */
    @GetMapping("/updateFile")
    @Operation(summary = "本地同步编辑过后的文件")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult updateFile(@RequestParam("fileVersionId") String fileVersionId) {
        return yozoApi.updateFile(fileVersionId);
    }

    /**
     * 本地预览
     *
     * @param filename    名称
     * @param params      预览模型
     * @param previewType 类型
     * @return
     */
    @GetMapping("/storageFilePreview")
    @Operation(summary = "本地预览")
    @Parameters({
            @Parameter(name = "filename", description = "名称"),
            @Parameter(name = "previewType", description = "类型"),
    })
    public Object OnlinePreview(@RequestParam("filename") String filename, SuffixParams params, @RequestParam("previewType") String previewType) throws UnsupportedEncodingException {
        String url;
        String fileName = filename.substring(0, filename.lastIndexOf("."));
        String suffix = filename.substring(filename.lastIndexOf(".") + 1);
        if (previewType.equals(FilePreviewTypeEnum.YOZO_ONLINE_PREVIEW.getType())) {
            url = YozoParams.JNPF_DOMAINS + "/api/extend/DocumentPreview/down/" + fileName + "/" + suffix;
        } else {
            url = YozoParams.JNPF_DOMAINS + "/api/extend/DocumentPreview/pointDown/" + fileName + "." + suffix;
            url = URLEncoder.encode(url, "utf-8");
        }
        YozoFileParams fileParams = new YozoFileParams();
        BeanUtils.copyProperties(params, fileParams);
        fileParams.setUrl(url);
        return this.previewType(previewType, fileParams);
    }

    /**
     * 选择预览方式 yozo:永中预览;  doc:kk文档预览;
     * 说明 采用永中url方式预览时，请注意域名是否对应
     *
     * @param previewType
     * @param params
     * @return
     */
    public ActionResult previewType(String previewType, YozoFileParams params) {
        if (params.getUrl() == null) {
            return ActionResult.fail(MsgCode.ETD115.get());
        }
        if (previewType.equals(FilePreviewTypeEnum.YOZO_ONLINE_PREVIEW.getType())) {
            return yozoApi.getUrl(params);
        }
        //加密
        byte[] sb = params.getUrl().getBytes();
        String s = new String(Base64.encodeBase64(sb));
        if (previewType.equals(FilePreviewTypeEnum.LOCAL_PREVIEW.getType())) {
            String url = YozoParams.JNPF_DOMAINS + "/api/file/onlinePreview?url=" + s;
            return ActionResult.success("success", url);
        }
        return ActionResult.fail(MsgCode.ETD116.get());
    }
}
